package com.czy.seed.mvc.util;

import com.czy.seed.mvc.home.entity.Merchant;
import com.czy.seed.mvc.home.service.MerchantService;
import com.czy.seed.mvc.sys.entity.SysUser;
import com.czy.seed.mvc.sys.service.SysUserService;
import com.czy.seed.mybatis.base.QueryParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;
@RestController
public class ManageSendMessage {
//    @Autowired
//    private SysUserService sysUserService;
    @Autowired
    SystemProperties systemProperties;
    @Autowired
    private MerchantService merchantService;
    public void queryMessage(String queryTye, long userid, String context,SysUserService sysUserService,SystemProperties systemProperties) {
        String phone = "";
        String uids = "";
        SimpleDateFormat stringDf = new SimpleDateFormat("yyyy-MM-dd");
        //通过采购单位给采购办发短信  userid是采购单位的ID
        if ("采购办".equals(queryTye) && userid != 0) {
            try {
                SysUser  sysUser = sysUserService.selectByPrimaryKey(userid);
                QueryParams queryParams = new QueryParams(SysUser.class);
                QueryParams.Criteria criteria = queryParams.createCriteria();
                criteria.andEqualTo("id", sysUser.getCreateBy());
                SysUser sysUser1 = sysUserService.selectOneByParams(queryParams);
                phone = sysUser1.getTelephone();
                NotePort notePort = new NotePort();
                if (!"".equals(phone) && !"".equals(context) && systemProperties.getFlag()) {
                    notePort.SendMessage(uids, systemProperties.getAccount(), systemProperties.getPassword(), phone, context, stringDf.format(new Date()));
                } else {

                }
            } catch (Exception e) {
                throw new RuntimeException("流转单短信发送异常[采购办]");
            }
        }
        //给采购单位发短信,userid 是采购班ID，通过采购单就可以找到采购单位ID
        if("采购单位".equals(queryTye)&& userid != 0){
            NotePort notePort = new NotePort();
            try {
                if (!"".equals(phone) && !"".equals(context) && systemProperties.getFlag()) {
                    SysUser sysUser2 = sysUserService.selectByPrimaryKey(userid);
                    QueryParams queryParams = new QueryParams(SysUser.class);
                    QueryParams.Criteria criteria = queryParams.createCriteria();
                    criteria.andEqualTo("createBy", sysUser2.getCreateBy());
                    SysUser sysUser3 = sysUserService.selectOneByParams(queryParams);
                    phone = sysUser3.getTelephone();
                    notePort.SendMessage(uids, systemProperties.getAccount(), systemProperties.getPassword(), phone, context, stringDf.format(new Date()));
                } else {

                }
            }catch (Exception e){
                throw new RuntimeException("流转单短信发送异常[采购单位]");
            }
        }

        if("商家".equals(queryTye)&& userid != 0){
            Merchant merchant=null;
            NotePort notePort = new NotePort();
           try {

               merchant = merchantService.selectByPrimaryKey(userid);
               if (merchant != null) {
                   phone = merchant.getTel();
                   notePort.SendMessage(uids, systemProperties.getAccount(), systemProperties.getPassword(), phone, context, stringDf.format(new Date()));

               } else {
                   QueryParams queryParams = new QueryParams(Merchant.class);
                   QueryParams.Criteria criteria = queryParams.createCriteria();
                   criteria.andEqualTo("userId", userid);
                   merchant = merchantService.selectOneByParams(queryParams);
                   phone = merchant.getTel();
                   if (!"".equals(phone) && !"".equals(context) && systemProperties.getFlag()) {
                       notePort.SendMessage(uids, systemProperties.getAccount(), systemProperties.getPassword(), phone, context, stringDf.format(new Date()));
                   } else {

                   }
               }
           }catch (Exception e){
               throw new RuntimeException("流转单短信发送异常[商家]");
           }
        }
    }
}